﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SoftwareConsulting.BI11.Studio.UI.ProjectExplorer;
using SoftwareConsulting.BI11.Studio.UI.Properties;
using SoftwareConsulting.BI11.Studio.InternalServices;

namespace SoftwareConsulting.BI11.Studio.UI
{
    public partial class SettingsDialog : Form
    {
        private ProjectExplorerForm _projectExplorerLink; //ссылка на форму обозревателя проекта
        private bool _initialized; //флаг проинициализированного диалога
        private PanelsStyle _beforeStyle; //предыдущее оформление панелей

        /// <summary>
        /// Конструктор класса        
        /// </summary>
        /// <param name="projectExplorerLink"></param>
        public SettingsDialog(ProjectExplorerForm projectExplorerLink)
        {
            _projectExplorerLink = projectExplorerLink;
            InitializeComponent();
        }

        /// <summary>
        /// Инициализация диалога
        /// </summary>
        private void Initialize()
        {
            _beforeStyle = _projectExplorerLink.Style;
            AutoopenBox.Checked = _projectExplorerLink.CurrentSettings.Autoopen;
            BlockCharacteristicsBox.Checked = _projectExplorerLink.CurrentSettings.CharacteristicsBlockingAbility;
            SoundsBox.Checked = _projectExplorerLink.CurrentSettings.PlaySounds;
            AutohideKindsBox.Checked = _projectExplorerLink.CurrentSettings.AutohideObjectsGroups;
            SimpleBox.Checked = _projectExplorerLink.CurrentSettings.SimpleMode;
            var items = StyleBox.Properties.Items;
            items.Clear();
            items.Add(Resources.BI_STYLE_CARAMEL);
            items.Add(Resources.BI_STYLE_MONEYTWINS);
            items.Add(Resources.BI_STYLE_LILIAN);
            items.Add(Resources.BI_STYLE_THEASPHALTWORLD);
            items.Add(Resources.BI_STYLE_IMAGINARY);
            items.Add(Resources.BI_STYLE_BLACK);
            items.Add(Resources.BI_STYLE_BLUE);
            int index = (int)_projectExplorerLink.Style - 1;
            StyleBox.SelectedIndex = index;
            double maxerror = _projectExplorerLink.CurrentSettings.DiscriminantAnalysisMaxError;
            maxerror = maxerror < 0 ? 0 : maxerror > 1 ? 1 : maxerror;
            DiscriminantAnalysisMaxErrorBox.Value = Convert.ToInt32(Math.Round(maxerror * 100));
        }

        /// <summary>
        /// Применяет изменения и закрывает диалог
        /// </summary>
        private void ApplyAndClose()
        {
            _projectExplorerLink.CurrentSettings.Autoopen = AutoopenBox.Checked;
            _projectExplorerLink.CurrentSettings.CharacteristicsBlockingAbility = BlockCharacteristicsBox.Checked;
            _projectExplorerLink.CurrentSettings.PlaySounds = SoundsBox.Checked;
            _projectExplorerLink.CurrentSettings.AutohideObjectsGroups = AutohideKindsBox.Checked;
            _projectExplorerLink.CurrentSettings.DiscriminantAnalysisMaxError = Convert.ToDouble(DiscriminantAnalysisMaxErrorBox.Value) / 100;
            _projectExplorerLink.SimpleMode = SimpleBox.Checked;
            DialogResult = DialogResult.OK;
            Close();
        }

        /// <summary>
        /// Отменяет изменения и закрывает диалог
        /// </summary>
        private void CancelAndClose()
        {
            _projectExplorerLink.Style = _beforeStyle;
            Close();
        }

        /// <summary>
        /// Предпросмотр
        /// </summary>
        private void Preview()
        {            
            if (_initialized)
            {
                switch (StyleBox.SelectedIndex)
                {
                    case 0:
                        _projectExplorerLink.Style = PanelsStyle.Caramel;
                        break;
                    case 1:
                        _projectExplorerLink.Style = PanelsStyle.MoneyTwins;
                        break;
                    case 2:
                        _projectExplorerLink.Style = PanelsStyle.Lilian;
                        break;
                    case 3:
                        _projectExplorerLink.Style = PanelsStyle.TheAsphaltWorld;
                        break;
                    case 4:
                        _projectExplorerLink.Style = PanelsStyle.IMaginary;
                        break;
                    case 5:
                        _projectExplorerLink.Style = PanelsStyle.Black;
                        break;
                    case 6:
                        _projectExplorerLink.Style = PanelsStyle.Blue;
                        break;
                }
            }
        }

        private void OKButton_Click(object sender, EventArgs e)
        {
            ApplyAndClose();
        }

        private void CancelSettingsButton_Click(object sender, EventArgs e)
        {
            CancelAndClose();
        }

        private void SettingsDialog_Load(object sender, EventArgs e)
        {
            _initialized = false;
            Initialize();
            _initialized = true;
        }

        private void StyleBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            Preview();
        }

        /// <summary>
        /// Устанавливает стиль панелей
        /// </summary>
        /// <param name="style"></param>
        public void SetPanelsStyle(PanelsStyle style)
        {
            string skinName = "Caramel";
            switch (style)
            {
                case PanelsStyle.None:
                    Assert.Fail(Assert.Id.Assert00179);
                    break;
                case PanelsStyle.Caramel:
                    skinName = Resources.BI_STYLE_CARAMEL;
                    _projectExplorerLink.SetRibbonsBlackTheme(false);
                    break;
                case PanelsStyle.MoneyTwins:
                    skinName = Resources.BI_STYLE_MONEYTWINS;
                    _projectExplorerLink.SetRibbonsBlackTheme(false);
                    break;
                case PanelsStyle.Lilian:
                    skinName = Resources.BI_STYLE_LILIAN;
                    _projectExplorerLink.SetRibbonsBlackTheme(false);
                    break;
                case PanelsStyle.TheAsphaltWorld:
                    skinName = Resources.BI_STYLE_THEASPHALTWORLD;
                    _projectExplorerLink.SetRibbonsBlackTheme(false);
                    break;
                case PanelsStyle.IMaginary:
                    skinName = Resources.BI_STYLE_IMAGINARY;
                    _projectExplorerLink.SetRibbonsBlackTheme(false);
                    break;
                case PanelsStyle.Black:
                    skinName = Resources.BI_STYLE_BLACK;
                    _projectExplorerLink.SetRibbonsBlackTheme(true);
                    break;
                case PanelsStyle.Blue:
                    skinName = Resources.BI_STYLE_BLUE;
                    _projectExplorerLink.SetRibbonsBlackTheme(false);
                    break;
            }
            DefaultLookAndFeelCustomizer.LookAndFeel.SkinName = skinName;
        }
    }
}
